package com.co.server.service.vote.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.co.common.dao.BaseDao;
import com.co.common.service.BaseServiceImpl;
import com.co.server.dao.vote.TVoteItemDao;
import com.co.server.entity.vote.TVoteItem;
import com.co.server.entity.vote.aide.TVoteItemVo;
import com.co.server.entity.vote.aide.TVoteUserQuery;
import com.co.server.service.vote.TVoteItemService;
import com.co.server.service.vote.TVoteUserService;

@Service
public class TVoteItemServiceImpl extends BaseServiceImpl<TVoteItem, Integer> implements TVoteItemService {
    @Resource
    private TVoteItemDao tVoteItemDao;
    
    @Resource
    private TVoteUserService tVoteUserService;

    @Override
    protected BaseDao<TVoteItem, Integer> getBaseDao() {
        return tVoteItemDao;
    }

	public void setPrecent(Integer voteId, List<TVoteItem> voteItemList) {
		List<Map<String,Object>> queryItemCount = tVoteUserService.queryItemCount(voteId);
		TVoteUserQuery tVoteUserQuery = new TVoteUserQuery();
		tVoteUserQuery.setVoteId(voteId);
		TVoteItemVo tVoteItemVo = null;
		Long count = 0l;
		long precent = 0l;
		long userCount = tVoteUserService.queryCount(tVoteUserQuery);
		for (TVoteItem tVoteItem : voteItemList) {
			for (Map<String,Object> map : queryItemCount) {
				int voteItemId = ((Long) map.get("voteItemId")).intValue();
				int id = tVoteItem.getId();
				if(voteItemId == id){
					count = (Long) map.get("count");
					precent = count*100/userCount;
					tVoteItemVo=(TVoteItemVo) tVoteItem;
					tVoteItemVo.setUserPrecent(precent+"%");
				}
			}
		}
	}
}